#include"BallMover.h"
#include"Box2D/Box2D.h"
#include"cocos2d.h"
#include<cmath>

USING_NS_CC;

BallMover::BallMover(b2Body * ball)
{
	this->ball = ball;
	ball->SetLinearDamping(0.5);
	torque = 5;
	speed.Set(5, 0);
}

BallMover::~BallMover()
{
}

void BallMover::update()
{
	if (isMove)
	{
		ball->SetLinearVelocity(speed);
	}
}

void BallMover::onKeyDown(int keyCode)
{
	switch ((EventKeyboard::KeyCode)(keyCode))
	{
	case EventKeyboard::KeyCode::KEY_LEFT_ARROW:
		isMove = true;
		speed.x = -abs(speed.x);
		torque = -abs(torque);
		break;
	case EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
		isMove = true;
		speed.x = abs(speed.x);
		torque = abs(torque);
		break;
	default:
		break;
	}
}

void BallMover::onKeyUp(int KeyCode)
{
	isMove = false;
	switch ((EventKeyboard::KeyCode)(KeyCode))
	{
	case EventKeyboard::KeyCode::KEY_UP_ARROW:
	{
		if (isReadyJump)
		{
			b2Vec2 force(0, 10);
			ball->ApplyLinearImpulse(force, ball->GetPosition(), true);
		}
	}
		break;
	default:
		break;
	}
	if (isReadyJump)
	{
		
	}
}
